package com.spark.gaea.config.service.easyzk;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;

public final class ConfigFactory {
	private ConfigProfile configProfile;
	private ConfigLocalCache configLocalCache;
	private static final Logger LOG = LoggerFactory.getLogger(ConfigFactory.class);

	public ConfigFactory(final String connectStr, final String rootNode) {
		this(connectStr, rootNode, false);
	}

	public ConfigFactory(final String connectStr, final String rootNode, final boolean openLocalCache) {
		this.configProfile = new ConfigProfile(connectStr, rootNode);
		if (openLocalCache) {
			this.configLocalCache = new ConfigLocalCache(rootNode);
		}
	}

	public ConfigNode getConfigNode(final String node) {
		return this.getConfigNode(node, ConfigNode.KeyLoadingMode.NONE, null);
	}

	public ConfigNode getConfigNode(final String node, final ConfigNode.KeyLoadingMode keyLoadingMode,
			final Set<String> keysSpecified) {
		if (ConfigFactory.LOG.isDebugEnabled()) {
			ConfigFactory.LOG.debug("Get node[{}] with mode[{}] and keys[{}]", node, keyLoadingMode, keysSpecified);
		}
		final ConfigNode configNode = new ConfigNode(this.configProfile, node);
		configNode.defineKeyLoadingPattern(keyLoadingMode, keysSpecified);
		configNode.setConfigLocalCache(this.configLocalCache);
		configNode.initConfigNode();
		return configNode;
	}
}
